package listener;

import service.CategoryService;
import service.GoodsService;
import service.OrderService;
import service.UserService;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener()
public class ApplicationListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
    CategoryService tsService=new CategoryService();
    GoodsService gsService=new GoodsService();
    UserService usService=new UserService();
    OrderService orService=new OrderService();
    // Public constructor is required by servlet spec
    public ApplicationListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */

//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
//        int userId = user.getId();
        sce.getServletContext().setAttribute("categoryList",tsService.GetAllType());
        sce.getServletContext().setAttribute("goodsList",gsService.GetAllProducts());
        sce.getServletContext().setAttribute("userList",usService.GetAllUser());
        sce.getServletContext().setAttribute("orderList",orService.GetAllOrder());
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}

